﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class AddBatchConnectArViewModel:ViewModelBase
    {
        private GWCmdMakeUpService gWCmdMakeUpService = new GWCmdMakeUpService();
        public static GWCmdMakeUp GWCmdMakeUp;

        private int totalNum;

        public int TotalNum
        {
            get { return totalNum; }
            set { totalNum = value; RaisePropertyChanged(); SaveDataInfo(); GetDataInfo(value); }
        }

        private List<int> totalNumList;

        public List<int> TotalNumList
        {
            get { return totalNumList; }
            set { totalNumList = value; RaisePropertyChanged(); }
        }

        private string groupName;

        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; RaisePropertyChanged(); }
        }


        private ObservableCollection<GWCmdMakeUp> gWCmdMakeUpList;

        public ObservableCollection<GWCmdMakeUp> GWCmdMakeUpList
        {
            get { return gWCmdMakeUpList; }
            set { gWCmdMakeUpList = value; RaisePropertyChanged(); }
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; RaisePropertyChanged(); GroupName=$"第{value+1}个配置单元"; UpDataInfo(); }
        }


        private int configNum;

        public int ConfigNum
        {
            get { return configNum; }
            set { configNum = value; RaisePropertyChanged(); }
        }

        private string address;

        public string Address
        {
            get { return address; }
            set { address = value; RaisePropertyChanged(); }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> baudRateList;

        public ObservableCollection<string> BaudRateList
        {
            get { return baudRateList; }
            set { baudRateList = value; RaisePropertyChanged(); }
        }

        private int rateNum;

        public int RateNum
        {
            get { return rateNum; }
            set { rateNum = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> agreementTypeList;

        public ObservableCollection<string> AgreementTypeList
        {
            get { return agreementTypeList; }
            set { agreementTypeList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> portList;

        public ObservableCollection<string> PortList
        {
            get { return portList; }
            set { portList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> connectTypeList;

        public ObservableCollection<string> ConnectTypeList
        {
            get { return connectTypeList; }
            set { connectTypeList = value; RaisePropertyChanged(); }
        }

        private int userType;

        public int UserType
        {
            get { return userType; }
            set { userType = value; RaisePropertyChanged(); }
        }

        private double voltage;

        public double Voltage
        {
            get { return voltage; }
            set { voltage = value; RaisePropertyChanged(); }
        }

        private double current;

        public double Current
        {
            get { return current; }
            set { current = value; RaisePropertyChanged(); }
        }

        private int baudRateSelected;

        public int BaudRateSlected
        {
            get { return baudRateSelected; }
            set { baudRateSelected = value; RaisePropertyChanged(); }
        }

        private int agreementTypeSelected;

        public int AgreementTypeSlected
        {
            get { return agreementTypeSelected; }
            set { agreementTypeSelected = value; RaisePropertyChanged(); }
        }

        private int portSelected;

        public int PortSelected
        {
            get { return portSelected; }
            set { portSelected = value; RaisePropertyChanged(); }
        }

        private int connectTypeSelected;

        public int ConnectTypeSelected
        {
            get { return connectTypeSelected; }
            set { connectTypeSelected = value; RaisePropertyChanged(); }
        }

        private string tAddress;

        public string TAddress
        {
            get { return tAddress; }
            set { tAddress = value; RaisePropertyChanged(); }
        }

        private string asset;

        public string Asset
        {
            get { return asset; }
            set { asset = value; RaisePropertyChanged(); }
        }

        private string pt;

        public string PT
        {
            get { return pt; }
            set { pt = value; RaisePropertyChanged(); }
        }

        private string ct;

        public string CT
        {
            get { return ct; }
            set { ct = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<RCSDDataModel> addDataList;

        public ObservableCollection<RCSDDataModel> AddDataList
        {
            get { return addDataList; }
            set { addDataList = value; RaisePropertyChanged(); }
        }

        public AddBatchConnectArViewModel()
        {
            BaudRateList = new ObservableCollection<string>() { "300", "600", "1200", "2400", "4800", "7200", "9600", "19200", "38400", "57600", "115200", "自适应" };
            AgreementTypeList = new ObservableCollection<string>() {
                "DL/T 645-1997" ,
                "DL/T 645-2007",
                "本部分",
                "CT/T 188-2004",
            };
            ConnectTypeList = new ObservableCollection<string>() { "未知", "单相", "三相三线", "三相四线" };
            PortList = new ObservableCollection<string>() { "交采", "485-1", "485-2", "485-3", "载波" };
            TotalNumList = new List<int>() { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
            AddDataList = new ObservableCollection<RCSDDataModel>();
        }


        public void GetDataInfo(int total)
        {
            GWCmdMakeUpList = new ObservableCollection<GWCmdMakeUp>(gWCmdMakeUpService.SelectByOAD("60008000", total));
            if (GWCmdMakeUpList.Count<=0)
            {
                return;
            }
            SelectedIndex = 0;
            UpDataInfo();
        }
        public void UpDataInfo()
        {
            if (SelectedIndex < 0)
            {
                SelectedIndex = 0;
            }
            if (GWCmdMakeUpList.Count<=0)
            {
                return;
            }
            //判断之前是否点击过，如果点击过则保存之前的数据
            if (GWCmdMakeUp != null)
            {
                SaveDataInfo();
            }

            GWCmdMakeUp = GWCmdMakeUpList[SelectedIndex];
            if (GWCmdMakeUp==null)
            {
                return;
            }
            try
            {
                //AddDataList.Clear();
                var datas = GWCmdMakeUp.DataValue.Split(';');
                ConfigNum = int.Parse(datas[0]);
                Address = datas[1];
                BaudRateSlected = Convert.ToInt16(datas[2]);
                AgreementTypeSlected = Convert.ToInt16(datas[3]);
                ConnectTypeSelected = Convert.ToInt16(datas[8]);
                switch (datas[4])
                {
                    case "F2080201":
                        PortSelected = 0;
                        break;
                    case "F2010201":
                        PortSelected = 1;
                        break;
                    case "F2010202":
                        PortSelected = 2;
                        break;
                    case "F2010203":
                        PortSelected = 3;
                        break;
                    case "F2090201":
                        PortSelected = 4;
                        break;
                    default:
                        break;
                }
                Password = datas[5];
                RateNum = Convert.ToInt16(datas[6]);
                UserType = Convert.ToInt16(datas[7]);
                Voltage = double.Parse(datas[9]);
                Current = double.Parse(datas[10]);
                TAddress = datas[11];
                Asset = datas[12];
                PT = datas[13];
                CT = datas[14];
                /*var adds = datas[15].Split(',');
                int num = adds.Length / 2;
                if (num <= 0)
                {
                    return;
                }
                for (int i = 0; i < num; i ++)
                {
                    RCSDDataModel rCSDDataModel = new RCSDDataModel();
                    rCSDDataModel.CSD = adds[2*i];
                    rCSDDataModel.Data = adds[2*i + 1];
                    AddDataList.Add(rCSDDataModel);
                }*/
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            
            
        }

        public void SaveDataInfo()
        {
            if (GWCmdMakeUp==null)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(ConfigNum);
            stringBuilder.Append(";");
            stringBuilder.Append(Address);
            stringBuilder.Append(";");
            stringBuilder.Append(BaudRateSlected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(AgreementTypeSlected.ToString());
            stringBuilder.Append(";");
            switch (PortSelected)
            {
                case 0:
                    stringBuilder.Append("F2080201");
                    break;
                case 1:
                    stringBuilder.Append("F2010201");
                    break;
                case 2:
                    stringBuilder.Append("F2010202");
                    break;
                case 3:
                    stringBuilder.Append("F2010203");
                    break;
                case 4:
                    stringBuilder.Append("F2090201");
                    break;
                default:
                    break;
            }
            stringBuilder.Append(";");
            stringBuilder.Append(Password);
            stringBuilder.Append(";");
            stringBuilder.Append(RateNum.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(UserType.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(ConnectTypeSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(Voltage.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(Current.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(TAddress);
            stringBuilder.Append(";");
            stringBuilder.Append(Asset);
            stringBuilder.Append(";");
            stringBuilder.Append(PT.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(CT.ToString());
            stringBuilder.Append(";");
            /*for (int i = 0; i < AddDataList.Count; i++)
            {
                RCSDDataModel rCSDDataModel = AddDataList[i];
                stringBuilder.Append(rCSDDataModel.CSD);
                stringBuilder.Append(",");
                stringBuilder.Append(rCSDDataModel.Data);
                stringBuilder.Append(",");
            }*/
            GWCmdMakeUp.DataValue = stringBuilder.ToString();
            gWCmdMakeUpService.Update(GWCmdMakeUp);
        }
    }
}
